package com.wsoft.bpm.controller;

import cn.hutool.json.JSONUtil;
import com.wsoft.bpm.dto.ActModelDTO;
import com.wsoft.bpm.dto.ActModelDataDTO;
import com.wsoft.bpm.entity.ActReModelEntity;
import com.wsoft.bpm.query.model.ActModelQuery;
import com.wsoft.bpm.service.IActReModelService;
import com.wsoft.bpm.service.IActWorkflowConfigService;
import com.wsoft.response.ResponsePage;
import com.wsoft.response.ResponseResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author zhonglj
 * @date 2024-09-19 16:05
 **/
@RestController
@Tag(name = "工作流模型管理")
@RequestMapping("/bpm/model")
public class ActReModelController {

    @Resource
    private IActReModelService actReModelService;
    @Resource
    private IActWorkflowConfigService actWorkflowConfigService;

    @Operation(summary = "流程模版列表", description = "流程模版列表")
    @PostMapping("/list")
    public ResponseResult<ResponsePage<ActModelDTO>> listModel(@RequestBody ActModelQuery reqVO) {
        return ResponseResult.success(ResponsePage.restPage(actReModelService.pageList(reqVO)), "[sys.msg.success]");
    }

    @Operation(summary = "流程模版详情", description = "流程模版详情")
    @GetMapping("/detail")
    public ResponseResult<ActModelDTO> detail(@RequestParam String modelId) {
        ActReModelEntity entity = actReModelService.getById(modelId);
        ActModelDTO detail = new ActModelDTO();
        detail.setId(entity.getId());
        detail.setName(entity.getName());
        detail.setKey(entity.getKey());
        detail.setCategory(entity.getCategory());
        String description = JSONUtil.parseObj(entity.getMetaInfo()).getStr("description");
        detail.setDesc(description);
        return ResponseResult.success(detail, "[sys.msg.success]");
    }

    @Operation(summary = "流程模版新增", description = "流程模版新增")
    @PostMapping("/create")
    public ResponseResult createModel(@RequestBody ActModelDTO modelDTO) {
        String modelId = actReModelService.createModel(modelDTO);
        actReModelService.recordModelKey(modelId);
        return ResponseResult.success("[sys.msg.success]");
    }

    @Operation(summary = "流程模版编辑", description = "流程模版新增")
    @PostMapping("/update")
    public ResponseResult updateModel(@RequestBody ActModelDTO modelDTO) {
        actReModelService.updateModel(modelDTO);
        actReModelService.recordModelKey(modelDTO.getId());
        return ResponseResult.success("[sys.msg.success]");
    }

    @Operation(summary = "流程模版删除", description = "流程模版删除")
    @GetMapping("/delete")
    public ResponseResult deleteModel(@RequestParam("id") String modelId) {
        actReModelService.deleteModel(modelId);
        return ResponseResult.success("[sys.msg.success]");
    }


    @Operation(summary = "流程设计数据保存", description = "流程设计数据保存")
    @PostMapping("/saveData")
    public ResponseResult saveModelData(@RequestBody ActModelDataDTO modelDataDTO) {
        actReModelService.saveModelData(modelDataDTO);
        return ResponseResult.success("[sys.msg.success]");
    }

    @Operation(summary = "模版设计预览", description = "模版设计预览")
    @GetMapping("/xmlView")
    public ResponseResult xmlView(String modelId) {
        String xmlView = actReModelService.xmlView(modelId);
        return ResponseResult.success(xmlView);
    }

    @Operation(summary = "部署流程定义", description = "部署流程定义")
    @PostMapping("/deploy")
    public ResponseResult deploy(String modelId) {
        String deploymentId = actReModelService.deploy(modelId);
        // 更新json配置表
        actWorkflowConfigService.updateAfterDeploy(modelId, deploymentId);
        return ResponseResult.success(deploymentId);
    }

}
